﻿@model iMyRestaurent.Models.ForwardModel
@section import {
    @NoCache.Instance.Css(Url, "~/Content/location.css")
}
@{
    ViewBag.Title = "iMyRestaurant - Location List";
    Layout = "~/Views/Shared/_Layout.cshtml";
    
}
<div id="title">@ViewBag.Title</div>
<div>
    <div class="opacity-box center-box" style="width: 900px; margin-top: 30px;">
        @if (Model.CanCreateLocation)
        {
            <form action="@Url.Action("Create", "Location")" method="post" style="margin-bottom: 20px;">
            <input type="hidden" name="Mode" value="@iMyRestaurent.Shared.Mode.CreateFromLocationList" />
            <input type="hidden" name="SummaryDate" value="@Model.SummaryDate" />
            <input type="hidden" name="SummaryLocationID" value="@Model.SummaryLocationID" />
            <input type="submit" class="image-button button-create-location" value="" />
            </form>
        }
        <div id="list-label" class="center-box">
            <span>Please select a location</span>
        </div>
        @if (Model.Locations != null && Model.Locations.LocationList != null)
        {
            <form id="forward-form" method="post" action="">
            <input type="hidden" name="LocationID" id="location-id" />
            <input type="hidden" name="LocationName" id="location-name" />
            <input type="hidden" name="ForwardTo" value="@Model.ForwardTo" />
            <input type="hidden" name="SummaryDate" value="@Model.SummaryDate" />
            <input type="hidden" name="Mode" value="@Model.Mode" />
            <input type="hidden" name="SummaryLocationID" value="@Model.SummaryLocationID" />
            <div class="location-list center-box -fl-ah" fla-step="10">
                @foreach (var location in Model.Locations.LocationList)
                {
                    <div id="@location.LocationID" class="clearfix location-row center-box">
                        <span>@location.Name</span>
                        <div class="delete-button right">
                        </div>
                    </div>
                }
            </div>
            </form>
        }
    </div>
</div>
@section footer {
    @using (Html.BeginForm("Index", "Summary"))
    {
        <input type="hidden" name="LocationID" value="@Model.SummaryLocationID" />
        <input type="hidden" name="Date" value="@Model.SummaryDate" />
        <div class="left clearfix">
            <input type="submit" value="" class="button-back image-button" />
        </div>
    }
}
@section script {
    <script type="text/javascript">
//        alert("previous url is: " + document.referrer);
        $(document).ready(function () {
            if (document.referrer.indexOf("Menu") != -1) {
                // from menu
                $('form[action="/Summary"]').attr('action', '/Menu');
            }
        });
        function deleteLocation(deleteButton) {
            var locationId = $(deleteButton).parent().attr("id");
            var locationName = $(deleteButton).parent().children("span").text();

            MessageBox.show({
                message: ["Do you want to delete " + locationName + "?"],
                buttons: {
                    no: null,
                    yes: function () {
                        // call api to check before delete this location
                        $.ajax({
                            async: false,
                            type: "post",
                            data: {
                                LocationID: locationId
                            },
                            url: '@Url.Action("CheckBeforeDelete", "Location")',
                            success: function (_check_result) {
                                if (_check_result.hasError) {
                                    MessageBox.show({
                                        message: ["Error while deleting"],
                                        buttons: {
                                            ok: null
                                        }
                                    });
                                } else {
                                    if (_check_result.hasBooking) {
                                        MessageBox.show({
                                            message: ["There are bookings made on this location. Would you like to delete it anyway?"],
                                            buttons: {
                                                no: null,
                                                yes: function () {
                                                    callDeleteLocationAPI(locationId, deleteButton);
                                                }
                                            }
                                        });
                                    } else {
                                        callDeleteLocationAPI(locationId, deleteButton);
                                    }
                                }
                            }
                        });
                    }
                }
            });
        }

        function callDeleteLocationAPI(locationId, deleteButton) {
            // call api to delete this location
            $.ajax({
                async: false,
                type: "post",
                data: {
                    LocationID: locationId
                },
                url: '@Url.Action("DeleteLocation", "Location")',
                success: function (_result) {
                    if (_result.isSucceed) {
                        $(deleteButton).parent().remove();
                    } else {
                        alert("Error");
                    }
                }
            });
        }

        function forward(locationRow) {
            $("#location-id").attr("value", locationRow.attr("id"));
            $("#location-name").attr("value", locationRow.children("span").text());

            var forwardTo = $('input[name="ForwardTo"]').attr("value");
            var form = $("#forward-form");

            // action = create-reservation
            if (forwardTo == "@iMyRestaurent.Models.ForwardAction.CreateReservation.ToString()") {
                // submit form to /Reservation/Create
                form.attr("action", '@Url.Action("Create", "Reservation")');
            } else if (forwardTo == "@iMyRestaurent.Models.ForwardAction.EditAvailability") {
                form.attr("action", '@Url.Action("Edit", "Availability")');
            } else if (forwardTo == "@iMyRestaurent.Models.ForwardAction.EditLocation") {
                form.attr("action", '@Url.Action("Edit", "Location")');
            } else if (forwardTo == "@iMyRestaurent.Models.ForwardAction.EditLayout") {
                form.attr("action", '@Url.Action("Edit", "Layout")');
            } else if (forwardTo == "@iMyRestaurent.Models.ForwardAction.EditBusinessHour") {
                form.attr("action", '@Url.Action("Edit", "BusinessHours")');
            }

            // add other forwarding actions here

            // submit form
            form.submit();
        }

        $(function () {
            Utils.touchScroll('.location-list');

            $(".location-row").click(function () {
                forward($(this));
            });

            $(".delete-button").click(function (e) {
                e.stopPropagation();

                deleteLocation($(this));
            });
        });
    </script>
}
